<?php


class KpisController extends AppController {
    var $name='Kpis';
    var $msg=null;
    var $uses=array('Kpi','Dmdform','Formstatus');
   

    function  beforeFilter() {
      
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
//            switch ($this->Auth->user('group_id')){
//                case 1:$this->Auth->allow('*');break;
//                case 2:$this->Auth->allow($this->allFunctions);break;
//                case 3:$this->Auth->allow($this->ItFunctions);break;
//                case 4:$this->Auth->allow($this->ItFunctions);break;
//                case 5:$this->Auth->allow($this->DemanderFunctions);break;
//                case 7:$this->Auth->allow($this->ItFunctions);break;
//                case 6:$this->Auth->allow(array('home'));break;
//            }
            $this->set('active',4);
            $this->prm07er(1);
            
        }

    function countForms(){

        //nb per Type
        $total=0;
        $total=0;
        $types=$this->Formstatus->Dmdform->Dmdformtype->getTypes();
        foreach($types as $key => $title){
            $nbTT[$title]=$this->Formstatus->getCountForTypes($key);
            $total+=$nbTT[$title];
        }
        $nbTT['Total']=$total;
        $this->set('types',$nbTT);

        //nb  per month and type
        foreach($this->Gestdate->numMoisM() as $key => $val){
            $total=0;
            foreach($types as $skey => $title){
                $nbT[$val][$title]=$this->Formstatus->getCountForTypesPerMonth($skey,$key);
                (empty($nbT[$val][$title]))?$nbT[$val][$title]=0:'';
                 $total+=$nbT[$val][$title];
            }
            $nbT[$val]['Total']=$total;
        }
        $this->set('moisT',  $nbT);

        //nb per Status
        $total=0;
        $statuse=$this->Formstatus->Dmdformstatus->getStatuses();
        foreach($statuse as $key => $title){
            $nb[$title]=$this->Formstatus->getCountForStatus($key);
            $total+=$nb[$title];
        }
        $nb['Total']=$total;
        $this->set('statuses',$nb);

        //nb  per month and status
        foreach($this->Gestdate->numMoisM() as $key => $val){
            $total=0;
            foreach($statuse as $skey => $title){
                $nbM[$val][$title]=$this->Formstatus->getCountForStatusPerMonth($skey,$key);
                (empty($nbM[$val][$title]))?$nbM[$val][$title]=0:'';
                 $total+=$nbM[$val][$title];
            }
            $nbM[$val]['Total']=$total;
        }
        $this->set('mois',  $nbM);       

        //nb users per jp
         $total=0;
        $jobs=$this->Dmdform->User->Jobprofile->getJobList();
        foreach ($jobs as $key => $value) {
            $nbJ[$value]=$this->Dmdform->User->getUsersPerJobprofile($key);
            $total+=$nbJ[$value];
        }
        $nbJ['Total']=$total;
        $this->set('jobs',$nbJ);

        //nb Risks
        $this->set('risks',  $this->Formstatus->getRisksKPI());

        //closing

        $this->set('days',  $this->Formstatus->getProcessAverage());
    }



    function _countFormsPerType($dmdType,$month=null,$year=null){
        if (empty($month) && empty($year)){
            return $this->Formstatus->getCountForTypes($dmdType);
        }elseif(!empty($month) && empty($year)){
            return $this->Formstatus->getCountForTypesPerMonth($dmdType,$month);
        }elseif(empty($month) && !empty($year)){
            return $this->Formstatus->getCountForTypesPerYear($dmdType,$year);
        }elseif(!empty($month) && !empty($year)){
            return $this->Formstatus->getCountForTypesPerMonthAndYear($dmdType,$month,$year);
        }
    }
    function _countFormsPerTypeInterval($dmdType,$date1=null,$date2=null){
        if(empty ($date1) && empty ($date2)){
            return $this->Formstatus->getCountForTypes($dmdType);
        }elseif(!empty($date1) && empty($date2)){
            return $this->Formstatus->getCountForTypesInterval($dmdType,$date1);
        }elseif(!empty ($date1) && !empty($date2)){
            return $this->Formstatus->getCountForTypesInterval($dmdType,$date1,$date2);
        }
    }
    function _countpermonth(){
        
            return $d;
    }

    function filterKpis(){
        if($this->data){
            $this->set('data',$this->data);
        }
        $this->set('types',$this->Formstatus->Dmdform->Dmdformtype->getTypes());
    }
    function test(){
       // $this->set('test',  $this->_countFormsPerTypeInterval(1,'2011-05-01'));
    }
    function retest(){
       // $this->set('test',  $this->_countFormsPerTypeInterval(1,'2011-05-01'));
    }

    //PRM05_KPI
    function formPerType($typeId=null){
        $types=$this->Formstatus->Dmdform->Dmdformtype->getTypes();
        if(empty($typeId)){
            $total=0;            
            foreach($types as $key => $title){
                $nbTT[$title]=$this->_countFormsPerType($key);
                $total+=$nbTT[$title];
            }
            $nbTT['Total']=$total;
            $this->set('types',$nbTT);

            //nb  per month and type
            foreach($this->Gestdate->numMoisM() as $key => $val){
                $total=0;
                foreach($types as $skey => $title){
                    $nbT[$val][$title]=$this->_countFormsPerType($skey,$key);
                    (empty($nbT[$val][$title]))?$nbT[$val][$title]=0:'';
                     $total+=$nbT[$val][$title];
                }
                $nbT[$val]['Total']=$total;
            }
            $this->set('moisT',  $nbT);

        }else{
            $nbTT[$types[$typeId]]=$this->_countFormsPerType($typeId);
            $nbTT['Total']=$nbTT[$types[$typeId]];
            $this->set('types',$nbTT);
            foreach($this->Gestdate->numMoisM() as $key => $val){
                $total=0;

                    $nbT[$val][$types[$typeId]]=$this->_countFormsPerType($typeId,$key);
                    $total+=$nbT[$val][$types[$typeId]];
                $nbT[$val]['Total']=$total;
            }
            $this->set('moisT',  $nbT);
        }
    }
    
    
    function  showKpis_old(){
        $kpiData=array();
        $kpiData=array_merge($kpiData,$this->prm04kpi(true));
        $kpiData=array_merge($kpiData,$this->prm05kpi(true));
        $this->set('kpiData',$kpiData);
    }




    /*
     ***************************************************************  
     *************************************************************** 
     ********                                               ********
     ********               EXCEPTION REPORTS               ********
     ********                                               ********
     *************************************************************** 
     ***************************************************************
     */
    
    /*
     *      PRM01_ER
     */
    
    function prm01er(){
        $this->set('risks',  $this->Formstatus->getRisksKPI());
    }
    
    /*
     *      PRM07_ER
     */
    
    function prm07er($sendmail=0){
        
        $users=$this->Dmdform->User->find('all',array(
            'recursive'=>1,
            'conditions'=>array('User.contract_end BETWEEN ? AND ?'=>array(date('Y-m-d',time()+(86400*8)),date('Y-m-d',time()+(86400*9))))
        ));
        //$allUsers=  $this->Dmdform->User->find('all',array('recursive'=>-1));
        if($sendmail==1){
            $sentToday=$this->Dmdform->Action->find('count',array(
                    'conditions'=>array(
                        'Action.created BETWEEN ? AND ?'=>array(
                            date('Y-m-d 00:00:00'),
                            date('Y-m-d 23:59:59')
                            ),
                        'Action.actiontype_id'=>'5'
                        ),                     
                ));
            if ($sendmail<1){
                foreach ($users as $val) {
                    $this->Sendmail->customMail($val['Manager']['mail'],'The account of User: '.$val['User']['UsName'].' will expire in 8 days !','custom',$val['User']['UsName'].'\'s account will expire' );
                }
                return true;
            }else{
                return false;
            }
            
        }else{
            $this->set(compact('users'));
        }
        
    }
    
     /*
     ***************************************************************
     ***************************************************************
     ********                                               ********
     ********                     KPIS                      ********
     ********                                               ********
     ***************************************************************
     ***************************************************************
     */

    /*
     *  PRM04_KPI
     */

    function prm04kpi($r=false){
        $t=$this->Dmdform->query('select * from prm04kpi');
        $c=$this->Dmdform->Company->Country->find('list');
        $jp=$this->Dmdform->User->Jobprofile->find('list');
        $j=$this->Dmdform->User->Jobprofile->find('all',array('fields'=>array('title','family')));
        //$f=$this->Dmdform->User->Jobprofile->find('list',array('fields'=>array('distinct Jobprofile.family')));
        $f=$this->Dmdform->User->Jobprofile->find('all',array(
                'recursive'=>-1,
                'fields'=>array('DISTINCT family'),
            ));
            $f=Set::extract($f,"/Jobprofile/family");

        foreach($jp as $key => $val){
            $kpi[$val]=$this->Dmdform->query('select * from prm04kpi where prm04kpi.jp_id ='.$key);
        }
        
        $kpiData['k']=$kpi;
        $kpiData['c']=$c;
        $kpiData['j']=$j;
        $kpiData['t']=$t;
        $this->set('f',$f);
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }
        
    }
    
    function prm05kpi($r=false){
        $types=$this->Formstatus->Dmdform->Dmdformtype->getTypes();
        
        foreach ($types as $typeKey => $typeName){
            foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                $nbT[$typeKey][$monthKey]=$this->Formstatus->getCountForTypesPerMonthAndYear($typeKey,$monthKey,date('Y'));
            }
            $nbT[$typeKey]['n']=$this->Formstatus->getCountForTypesPerYear($typeKey,date('Y'));
            $nbT[$typeKey]['n-1']=$this->Formstatus->getCountForTypesPerYear($typeKey,date('Y')-1);
        }
        
        $kpiData=array('nbT'=>$nbT,'types'=>$types, 'months'=>$this->Gestdate->numMoisM());
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }

    }
    function prm05kpiForCountry($r=false,$country=null){
        $types=$this->Formstatus->Dmdform->Dmdformtype->getTypes();
        $countries=$this->Formstatus->Dmdform->Company->Country->find('list');
        foreach($countries as $countId => $countVal){
            foreach ($types as $typeKey => $typeName){
                foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                    $nbT[$typeKey][$monthKey]=$this->Formstatus->getCountForTypesPerMonthAndYearAndCountry($typeKey,$monthKey,date('Y'),$countId);
                }
                $nbT[$typeKey]['n']=$this->Formstatus->getCountForTypesPerYearAndCountry($typeKey,date('Y'),$countId);
                $nbT[$typeKey]['n-1']=$this->Formstatus->getCountForTypesPerYearAndCountry($typeKey,date('Y')-1,$countId);
            }
            $nbC[$countVal]=$nbT;
        }
        
        
        $kpiData=array('nbC'=>$nbC,'countries'=>$countries,'types'=>$types, 'months'=>$this->Gestdate->numMoisM());
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }

    }
    function prm10kpi($r=false){
        $types=$this->Formstatus->Dmdformstatus->find('list');
        
        foreach ($types as $typeKey => $typeName){
            foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                $nbT[$typeKey][$monthKey]=$this->Formstatus->getCountForStatusPerMonthAndYear($typeKey,$monthKey,date('Y'));
            }
            $nbT[$typeKey]['n']=$this->Formstatus->getCountForStatusPerYear($typeKey,date('Y'));
            $nbT[$typeKey]['n-1']=$this->Formstatus->getCountForStatusPerYear($typeKey,date('Y')-1);
        }
        
        $kpiData=array('nbT'=>$nbT,'types'=>$types, 'months'=>$this->Gestdate->numMoisM());
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }

    }
    function prm10kpiForCountry($r=false,$country=null){
        $types=$this->Formstatus->Dmdformstatus->find('list');
        $countries=$this->Formstatus->Dmdform->Company->Country->find('list');
        foreach($countries as $countId => $countVal){
            foreach ($types as $typeKey => $typeName){
                foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                    $nbT[$typeKey][$monthKey]=$this->Formstatus->getCountForStatusPerMonthAndYearAndCountry($typeKey,$monthKey,date('Y'),$countId);
                }
                $nbT[$typeKey]['n']=$this->Formstatus->getCountForStatusPerYearAndCountry($typeKey,date('Y'),$countId);
                $nbT[$typeKey]['n-1']=$this->Formstatus->getCountForStatusPerYearAndCountry($typeKey,date('Y')-1,$countId);
            }
            $nbC[$countVal]=$nbT;
        }
        
        
        $kpiData=array('nbC'=>$nbC,'countries'=>$countries,'types'=>$types, 'months'=>$this->Gestdate->numMoisM());
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }

    }
    
    function prm06kpi($r=false){
        $kpiData['risks']=$this->Formstatus->getRisksKPI();
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }
       
    }
    function prm09kpiOld($r=false){
        $kpiData['days']=$this->Formstatus->getProcessAverageForYear(date('Y'));
        $kpiData['daysN-1']=$this->Formstatus->getProcessAverageForYear(date('Y')-1);
        foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                $kpiData['daysMN'][$monthKey]=$this->Formstatus->getProcessAverageForYear(date('Y'),$monthKey);
            }
        foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                $kpiData['daysMN-1'][$monthKey]=$this->Formstatus->getProcessAverageForYear(date('Y')-1,$monthKey);
            }
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }
       
    }
    function prm09kpi($r=false){
        $countries=$this->Formstatus->Dmdform->Company->Country->find('list');
        foreach($countries as $key => $val){
            foreach ($this->Gestdate->numMoisM() as $monthKey => $monthVal){
                $kpiData['prm09kpi'][$val][$monthKey]['N']=$this->Formstatus->getProcessAverageForYearAndMonthAndCountry($key,date('Y'),$monthKey);
                $kpiData['prm09kpi'][$val][$monthKey]['N-1']=$this->Formstatus->getProcessAverageForYearAndMonthAndCountry($key,date('Y')-1,$monthKey);                
            }            
            $kpiData['prm09kpi'][$val]['total']['N-1']=$this->Formstatus->getProcessAverageForYearAndCountry($key,date('Y')-1);
            $kpiData['prm09kpi'][$val]['total']['N']=$this->Formstatus->getProcessAverageForYearAndCountry($key,date('Y'));
        }        
        $kpiData['countries']=$countries;
        $kpiData['months']= $this->Gestdate->numMoisM();
        if ($r){
            return $kpiData;
        }else{
            $this->set('kpiData',  $kpiData);
        }
    }
    
    /*
     ***************************************************************
     ***************************************************************
     ********                                               ********
     ********                     KPIS                      ********
     ********                                               ********
     ***************************************************************
     ***************************************************************
     */

    /*
     *  Display Kpi's
     */
    
     function showKpis(){
         $kpis=array('prm04kpi','prm05kpi','prm05kpiForCountry','prm06kpi','prm09kpi','prm10kpi','prm10kpiForCountry');
         $ers=array('prm07er');
         if($this->data){
             
         }
         $this->set('kpiArray',$kpis);
         $this->set('erArray',$ers);
     }
     
     /*
      *  Export to Excel
      * 
      */
     
     function exportToExcel(){
         $this->render=null;
         $this->Excel->openXls('files/template.xls');
         $this->Excel->ajoutDonneeParColEtLigne(0,1,'test');
         $this->Excel->saveAsXls('test');
         $this->Excel->closeXls();
     }
}

    
?>
